

#include <Adafruit_NeoPixel.h>   //加载库文件
#include <EEPROM.h>
#define LED_PIN       6          //灯带引脚
#define LED_NUM       12         //灯珠数量
#define Bright        128         //灯珠亮度
#define DelayTime     5         //延时时间
int a1;
int a2=0;
int ld = Bright;
int interrupt;
int customize=205;
int zdld=10;//最低亮度
int zgld=220;//最高亮度
int s=1;//灯效
int adjust;//切换亮度
int touch; //检测触摸长短
int rgb1;

Adafruit_NeoPixel strip(LED_NUM, LED_PIN, NEO_GRB + NEO_KHZ800);  //使用RGB模式控制WS2812类型灯带

void setup() {
  ld=EEPROM.get(0, ld);
  
//Serial.println(ld);//串口显示现在第几灯效
s=EEPROM.get(4, s);
//Serial.println(s);//串口显示现在第几灯效
customize=EEPROM.get(8, customize);
interrupt=EEPROM.get(12, interrupt);
delay(1000);
  strip.begin();                  //初始化灯带
  strip.setBrightness(ld);    //设置亮度数值S(max = 255)
  strip.show();                   //灯带显示
  Serial.begin(9600);//串口通信

  pinMode(3  ,INPUT_PULLUP);//设置3为上拉引脚，高电平。中断引脚
  attachInterrupt(digitalPinToInterrupt(3),blink,RISING);//设置外部中断（外部中断引脚,中断后执行的函数程序，中断出发方式为低电平变为高电平触发）

  

}


void loop() {
  switch (s){
case 1 :s1();//灯效1
break;
case 2 :s2();
break;
case 3 :s3();
break;
case 4 :s4();
break;
case 5 :s5();
break;
case 6 :s6();
break;
case 7 :s7();
break;
}
}

void s1(){//灯效1：rgb红
Serial.println("中");//串口显示现在第几灯效
if(rgb1==0){
  strip.clear();//重置灯效
  Serial.println("灯效1");//串口显示现在第几灯效
 for (uint8_t i = 0; i < ld; i++) {
    strip.fill(strip.Color(255, 0, 0), 0, LED_NUM);
    strip.setBrightness(i);
    strip.show();
    delay(5);      
  }}
  rgb1=1;
  strip.fill(strip.Color(255, 0, 0), 0, LED_NUM);
  strip.show();}

void s2(){//灯效2：rgb蓝
if(rgb1==1){
  uint8_t i = ld;
  while ( i > 0) {
    strip.fill(strip.Color(255, 0, 0), 0, LED_NUM);
    strip.show();
    delay(5);    
    strip.setBrightness(i--);  
  }
  while (i<ld) {
    strip.fill(strip.Color(0, 0, 255), 0, LED_NUM);
    strip.show();
    delay(5);   
    strip.setBrightness(i++);}
  rgb1=0;}
  strip.fill(strip.Color(0, 0, 255), 0, LED_NUM);
  strip.show();
}



void s3(){//灯效2：rgb暖白
if(rgb1==0){
  uint8_t i = ld;
  while ( i > 0) {
    strip.fill(strip.Color(0, 0, 255), 0, LED_NUM);
    strip.show();
    delay(5);    
    strip.setBrightness(i--);  
  }
  while (i<ld) {
    strip.fill(strip.Color(248, 141, 30), 0, LED_NUM);
    strip.show();
    delay(5);   
    strip.setBrightness(i++);}
  rgb1=1;}
  strip.fill(strip.Color(248, 141, 30), 0, LED_NUM);
  strip.show();
}


void s4() {//彩虹渐变
  for (int j = 0; j < 256; j++) {
    //--------------灯珠序列-------灯珠颜色-------//
    for (int a =0;a<LED_NUM;a++){
    strip.setPixelColor(a,Rainbow(j & 255));}
    strip.show();
    delay(35);
    if (interrupt==1){
      j=257;
    }
  }
}


void s5() {//彩虹流水
   for (int j = 255; j > 0; j--) {          //颜色循环
    for (uint16_t i = 0; i < LED_NUM; i++) {         //数量循环
      strip.setPixelColor(i, Rainbow((i * 256 / LED_NUM) + j));
    }
    if(j==0){j=255;}
    strip.show();
    delay(15);
    if(interrupt==0){j=0;}
  }
}

void s6() {//自定义颜色
if(a1==0){
  Serial.println("取消中");//串口显示现在第几灯效
detachInterrupt(digitalPinToInterrupt(3));//关闭中断
Serial.println("取消中断");//串口显示现在第几灯效
for (int a =0;a<LED_NUM;a++){
    strip.setPixelColor(a,Rainbow(customize & 255));}
    strip.show();
a1=1;}
delay(200);
touch=digitalRead(3);
if(touch==1){
  delay(600);
  touch=digitalRead(3);
  if(touch==1){
   Serial.println("调色");//串口显示现在第几灯效 
for (int j = 0; j < 256; j++) {
    //--------------灯珠序列-------灯珠颜色-------//
    customize++;
    if (customize==256){customize=0;}
    for (int a =0;a<LED_NUM;a++){
    strip.setPixelColor(a,Rainbow(customize & 255));}
    strip.show();
    delay(35);
    touch=digitalRead(3);
    if (touch==0){
      j=257;
      touch=1;
      EEPROM.put(8, customize);
    }
  }}
  if(touch==0){
 attachInterrupt(digitalPinToInterrupt(3),blink,RISING);//设置外部中断（外部中断引脚,中断后执行的函数程序，中断出发方式为低电平变为高电平触发）
blink;}
  }}

void s7() {//自定义颜色
 for (int i = ld; i > 0; i--) {
  for (int a =0;a<LED_NUM;a++){
    strip.setPixelColor(a,Rainbow(customize & 255));}
    strip.setBrightness(i);
    strip.show();
    delay(20);       //变亮
    if(i<40){delay(20);}
    if(interrupt==1){
      
      i=0;
      a2=1;
     //strip.clear();//重置灯效全部熄灭状态
    }
  }
  
if(a2==0){
Serial.println(interrupt);//串口显示现在第几灯效
  for (int j = 0; j<ld; j++) {
    for (int a =0;a<LED_NUM;a++){
    strip.setPixelColor(a,Rainbow(customize & 255));}
    strip.setBrightness(j);
    strip.show();
    delay(20);       //变暗
    if(j<40){delay(20);}
     if(interrupt==1){
      j=300;
     // strip.clear();//重置灯效全部熄灭状态
    }
  }}
}







void blink(){//中断后执行的程序
Serial.println("触发中断");//串口显示现在第几灯效
for(int a=0;a<500;a++){delayMicroseconds(1000);}
touch=digitalRead(3);
if (touch==0){//短触切换灯效
s++;
if(s==2){interrupt=0;a2=0;}
if(s==4){rgb1=0;}
if(s==5){interrupt=1;a1=0;EEPROM.put(12, interrupt);}
if(s==6){interrupt=0;}
if(s==8){//回到灯效1
  s=1;
  interrupt=1;
  a1=0;
  
  //strip.clear();//重置灯效
}
Serial.println(s);//串口显示现在第几灯效

Serial.println("短触");//串口显示现在第几灯效
EEPROM.put(4, s);
EEPROM.put(12, interrupt);
}
if(touch==1){//长触调整亮度
adjust_the_brightness();
EEPROM.put(0, ld);
}


}



void adjust_the_brightness(){//调整亮度
Serial.println("长触");//串口显示现在第几灯效
switch (adjust){
case 0 : 
  while(touch==1 && ld>zdld){
 Serial.println(ld);//串口显示现在第几灯效
    strip.fill(strip.Color(0, 255, 0), 0, LED_NUM);//把所有rgb灯设置为绿色
    strip.setBrightness(ld--); //设置彩灯亮度
    strip.show(); //更新灯效
    touch=digitalRead(3);
    delay(10);
   }
break;

case 1 :
  while(touch==1 && ld<zgld){
 Serial.println(ld);//串口显示现在第几灯效
    strip.fill(strip.Color(0, 255, 0), 0, LED_NUM);//把所有rgb灯设置为绿色
    strip.setBrightness(ld++); //设置彩灯亮度
    strip.show(); //更新灯效
    touch=digitalRead(3);
    delay(10);
   }
break;
}adjust=!adjust;//取反
}


//------颜色循环函数------//
uint32_t Rainbow(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}













